package org.lanqiao.controller;

import com.alibaba.druid.sql.dialect.mysql.visitor.transform.FromSubqueryResolver;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Delete;
import org.lanqiao.dto.DishDto;
import org.lanqiao.entity.Category;
import org.lanqiao.entity.Dish;
import org.lanqiao.service.CategoryService;
import org.lanqiao.service.DishService;
import org.lanqiao.utils.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private CategoryService categoryService;


    /**
     * 添加菜品
     * @param dishDto
     * @return
     */
    @PostMapping
    public Result<String> add(@RequestBody DishDto dishDto){
        dishService.saveDishAndFlavor(dishDto);
        return Result.success("新增菜品成功");
    }

    /**
     * 分页查询菜品
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public Result<Page> page(int page , int pageSize , String name){

        //构造分页构造器对象
        Page<Dish> pageInfo = new Page<>(page , pageSize);
        Page<DishDto> dishDtoPage = new Page<>();

        //条件构造器
        QueryWrapper<Dish> queryWrapper = new QueryWrapper<>();

        //添加过滤条件
        queryWrapper.like(name != null , "name" , name);

        //根据更新时间降序排列
       // queryWrapper.orderByDesc("update_time");

        dishService.page(pageInfo , queryWrapper);

        //对象拷贝，将dish中的数据拷贝到dishDto中
        BeanUtils.copyProperties(pageInfo , dishDtoPage , "records");

        List<Dish> records = pageInfo.getRecords();

        List<DishDto> dishDtoList = new ArrayList<>();
        for (Dish record : records) {
            DishDto dishDto = new DishDto();
            //获取recoreds（分类集合）中每个菜品的id

            BeanUtils.copyProperties(record , dishDto);
            Long categoryId = record.getCategoryId();
            //根据菜品的id查找菜品分类名称
            Category category = categoryService.getById(categoryId);
            if(category != null){
                String name1 = category.getName();
                dishDto.setCategoryName(name1);
            }
            dishDtoList.add(dishDto);
        }

        dishDtoPage.setRecords(dishDtoList);

        return Result.success(dishDtoPage);

    }

    /**
     * 根据id查询菜品信息
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Result<DishDto> getDishAndFlavorById(@PathVariable Long id){
        DishDto dishDto = dishService.getDishAndFlavorById(id);
        return Result.success(dishDto);
    }

    /**
     * 修改菜品信息
     * @param dishDto
     * @return
     */
    @PutMapping
    public Result<String> updateDishAndFlavor(@RequestBody DishDto dishDto){
        dishService.updateDishAndFlavor(dishDto);
        return Result.success("修改菜品成功");
    }

    /**
     * 删除菜品
     * @param id
     * @return
     */
    @DeleteMapping
    public Result<String> deleteDishAndFlavorById(@RequestParam("ids") Long id){
        dishService.deleteDishAndFlavorById(id);
        return Result.success("删除菜品成功");
    }

    /**
     *修改菜单状态（起售或停售）
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public Result<String> updateStatusById(@PathVariable("status") int status ,  @RequestParam("ids") List<Long> ids){
        //查找到要修改状态的菜品信息
        for (Long id : ids) {
            Dish dish = dishService.getById(id);
            status = dish.getStatus();
            if(status == 1){
                dish.setStatus(0);
            }
            //获取当前菜品的状态
            else dish.setStatus(1);
            dishService.updateById(dish);
        }

        /*status = dish.getStatus();
        if(status == 1){
            dish.setStatus(0);
        }
        //获取当前菜品的状态
         dish.setStatus(1);*/
        return Result.success("修改状态成功");

    }

    /**
     * 根据分类id查询菜品
     * @param dish
     * @return
     */
    @GetMapping("/list")
    public Result<List<Dish>> selectDish(Dish dish){

        QueryWrapper<Dish> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(dish.getCategoryId() != null , "category_id" , dish.getCategoryId());
        queryWrapper.like(dish.getName() != null , "name" , dish.getName());
        //查询出状态为起售的
        queryWrapper.eq("status" , 1);

        List<Dish> list = dishService.list(queryWrapper);

        return Result.success(list);

    }
}
